import IPy
import subprocess
import threading
import time
result=list()
def ping_ip(ip):
    p=subprocess.Popen(["ping.exe",'-n','1',ip],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
    if "TTL" in p.stdout.read().decode('gbk'):
        result.append("{0}已被使用\n".format(ip))
    else:
        result.append("{0}未被使用\n".format(ip))
ipaddres=input("pls input ipaddress:")
mask=input("pls input subnetmask:")
subnet=("{0}/{1}".format(ipaddres,mask))
try:
    ips=IPy.IP(subnet)
except ValueError as VE:
    print("input error")
else:
    start=time.time()
    threadlist=[]
    for ip in ips:
        ip=str(ip)
        print(ip)
        tscan=threading.Thread(target=ping_ip,args=(ip,))
        threadlist.append(tscan)
    for t in threadlist:
        t.setDaemon(True)
        t.start()
    for t in threadlist:
        t.join()
    with open('ipscan.txt',mode='w',encoding='utf-8') as ipscan:
        for i in result:
            ipscan.write(i)
    print("EXEC: {0} s".format(time.time()-start))